package com.hyzx.qbasic.admin.domain.repository.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hyzx.qbasic.admin.model.po.ResourceInfoPO;
import com.hyzx.qbasic.admin.model.po.ResourcePO;
import com.hyzx.qbasic.admin.domain.dao.ResourceMapper;
import com.hyzx.qbasic.admin.domain.repository.IResourceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

/**
 * Created by 黄江华 on 2017/10/9
 */

@Repository
public class ResourceRepositoryImpl implements IResourceRepository {

    @Autowired
    private ResourceMapper resourceMapper;

    @Override
    public List<ResourcePO> getAllResourceList() {
        return resourceMapper.getAllResourceList();
    }

    @Override
    public Set<String> getPermissionList(Set<String> roleList) {
        return resourceMapper.getPermissionList(roleList);
    }

    @Override
    public Set<String> getPermittedUrlList(Set<String> roleList) {
        return resourceMapper.getPermittedUrlList(roleList);
    }

    @Override
    public List<ResourcePO> getMenuList(Set<String> roleList) {
        return resourceMapper.getMenuList(roleList);
    }

    @Override
    public List<ResourcePO> getFunctionList(Set<String> roleList) {
        return resourceMapper.getFunctionList(roleList);
    }

    @Override
    public PageInfo<ResourceInfoPO> getPagedResourceList(int pageNum, int pageSize, String resourceName, Integer resourceId) {
        PageInfo pageInfo = PageHelper.startPage(pageNum, pageSize)
                .doSelectPageInfo(() -> resourceMapper.getPagedResourceList(resourceName, resourceId));

        return pageInfo;
    }

    @Override
    public boolean addResource(ResourcePO resource) {
        return resourceMapper.addResource(resource) > 0;
    }

    @Override
    public ResourcePO getResourceInfo(int resourceId) {
        return resourceMapper.getResourceInfo(resourceId);
    }

    @Override
    public boolean editResource(ResourcePO resource) {
        return resourceMapper.editResource(resource) > 0;
    }

    @Override
    @Transactional
    public boolean deleteResource(List<Integer> resourceIdList) {
        //级联删除资源及其子资源
        int deleteResourceResult = resourceMapper.deleteResource(resourceIdList);
        resourceMapper.deleteResourceRoles(resourceIdList);

        return deleteResourceResult > 0;
    }

    @Override
    public List<Integer> getResourceRoles(String permission) {
        return resourceMapper.getResourceRoles(permission);
    }
}
